package com.dao;

import java.util.List;

import javassist.bytecode.stackmap.BasicBlock.Catch;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cache.ReadWriteCache.Item;

public class ContactDAOImpl extends HibernateUtil implements ContactDAO{
	public boolean addDAO(Contact contact) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try{
			
		session.save(contact);
		session.getTransaction().commit();
		return true;
			
		} catch(HibernateException e){
		e.printStackTrace();
		return false;
		}
		
		}

		public boolean deleteDAO(Long id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Contact contact = (Contact) session.load(Contact.class, id);
		
		try{
		if (null != contact) {
		session.delete(contact);
		}
		session.getTransaction().commit();
		return true;} catch(HibernateException e){
			e.printStackTrace();
			return false;
			
		}
		}
		
		public boolean editDAO(Contact contact) {
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			Contact contact3 = (Contact)session.merge(contact);
			//contact = (Contact)session.get(Contact.class,id);
			try{
			if (null != contact3) {
			session.saveOrUpdate(contact3);
			}
			session.getTransaction().commit();
			return true;
			
			}catch(HibernateException e){
				e.printStackTrace();
				return false;
			}
		}
		public List<Contact> listSearchContact(String firstname,String lastname, boolean inActive,String order,int startPage,int endPage ) {
       //order = " name";
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Contact> contacts = null;
		try {

			String ia = inActive ? "" : " AND active = 'YES'";
			Transaction transaction = session.beginTransaction();
			Query query = session
					.createQuery("from Contact where firstname LIKE '%" + firstname
							+ "%' AND lastname LIKE'%" + lastname + "%'" + ia + order);
			query = query.setFirstResult(startPage);
			query.setMaxResults(endPage);
			contacts = query.list();
		} catch (HibernateException e) {
		e.printStackTrace();
		session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return contacts;
		}
		
		public List<Contact> listAllDAO() {

			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			List<Contact> contacts = null;
			try {

			contacts = (List<Contact>) session.createQuery("from Contact").list();

			} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			}
			session.getTransaction().commit();
			return contacts;
			}
		
		public Contact listEditContactDAO(Long id) {

			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			Contact contact = null;
			try {

				contact = (Contact)session.get(Contact.class,id);

				} catch (HibernateException e) {
				e.printStackTrace();
				
				}
				
				return contact;
			
			}

}
